package com.dyson.client.redisson;

import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author:Dyson
 * @time:2019/9/27 17:05
 */
public class RClient {
    public static void main(String[] args) {
        //1.创建配置对象,流式取单机版进行配置
        Config config = new Config();
        config.useSingleServer().setAddress("redis://root@192.168.237.128:6379")
                .setPassword("root");
        //2.创建Redisson对象
        RedissonClient redissonClient = Redisson.create(config);
        //响应式连接对象
        //Redisson.createReactive(config);
        //执行业务操作, redission方法与redis命令不一致
        //String 类型,不一样的是这里获取的key桶~,相当于获取key对象,进行操作,可进行流式编写
        RBucket<Object> k1 = redissonClient.getBucket("k1");
        //存,取
        k1.set("v1");
        System.out.println(k1.get());
        //过期时间
        k1.expire(5, TimeUnit.SECONDS);
        //List类型
        RList<Object> k2 = redissonClient.getList("k2");
        k2.addAll(new ArrayList<>());
        k2.expire(5, TimeUnit.SECONDS);
        List<Object> objects = k2.readAll();
        objects.forEach(o -> System.out.println(o.toString()));
        //后续基本此模板

        //main线程退出需关闭连接
        redissonClient.shutdown();
    }
}
